package org.kingtop.sys;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 分页类，用于记录分页的相关信息  
 * @author 陈金波
 * @date   2017年12月23日 上午11:22:06    
 * @Copyright: 2017 chen jinbo. All rights reserved.
 */
public class PageBean<T> implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -242877322577619254L;
	
	/**
	 * 默认的分页条数
	 */
	public static final int DEFAULT_PAGE_SIZE = 10;
	
	/**
	 * 当前页
	 */
	private Integer pageIndex;

	/**
	 * 每页条数
	 */
	private Integer pageSize;
	
	/**
	 * 总页数
	 */
	private Integer pageCount;
	
	/**
	 * 总条数
	 */
	private Integer totalCount;
	
	/**
	 * 列表数据
	 */
	private List<T> items = new ArrayList<T>();
	
	/**
	 * 是否有下一页
	 */
	private Boolean hashNextPage;
	
	/**
	 * 是否有上一页
	 */
	private Boolean hasPreviousPage;
	
	/**
	 * 下一页的索引，如果没有下一页，则为null
	 */
	private Integer nextPageIndex;
	
	/**
	 * 上一页的索引，如果没有上一页，则为null
	 */
	private Integer previousPageIndex;

	/**
	 * 初始化参数
	 * @param pageIndex 页码
	 * @param pageSize 每页条数
	 * @param totalCount 总记录数
	 * @param items 每页的数据
	 */
	public PageBean(Integer pageIndex, Integer pageSize, Integer totalCount, List<T> items) {
		super();
		this.pageIndex = pageIndex;
		this.pageSize = pageSize;
		this.totalCount = totalCount;
		this.items = items;
	}
	
	/**
	 * 校正参数，计算其他参数
	 * @author 陈金波  
	 * @date   2017年12月23日 下午1:57:17  
	 * @return void      
	 * @throws
	 */
	public void init(){
		//校正参数
		if(this.pageIndex <= 0){
			this.pageIndex = 1;
		}
		if(this.pageSize <= 0){
			this.pageSize = DEFAULT_PAGE_SIZE;
		}
		if(this.totalCount < 0){
			this.totalCount = 0;
		}
		
		//计算总页数
		this.pageCount =  this.totalCount % this.pageSize == 0 ? this.totalCount / this.pageSize : this.totalCount / this.pageSize + 1;
		//校正页码
		if(this.pageIndex > this.pageCount) {
			this.pageIndex = this.pageCount;
		}
		//判断是否有上一页
		if(this.pageIndex > 1){
			this.hasPreviousPage = true;
			this.previousPageIndex = this.pageIndex - 1;
		}
		if(this.pageIndex <= 1){
			this.hasPreviousPage = false;
		}
		//判断是否有下一页
		if(this.pageIndex < this.pageCount){
			this.hashNextPage = true;
			this.nextPageIndex = this.pageIndex + 1;
		}
		if(this.pageIndex >= this.pageCount){
			this.hashNextPage = false;
		}
	}

	public Integer getPageIndex() {
		return pageIndex;
	}

	public void setPageIndex(Integer pageIndex) {
		this.pageIndex = pageIndex;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

	public Integer getPageCount() {
		return pageCount;
	}

	public Integer getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(Integer totalCount) {
		this.totalCount = totalCount;
	}

	public List<T> getItems() {
		return items;
	}

	public void setItems(List<T> items) {
		this.items = items;
	}

	public Boolean getHashNextPage() {
		return hashNextPage;
	}

	public Boolean getHasPreviousPage() {
		return hasPreviousPage;
	}

	public Integer getNextPageIndex() {
		return nextPageIndex;
	}

	public Integer getPreviousPageIndex() {
		return previousPageIndex;
	}
	
}
